package net.zjitc.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Administrator on 2017/11/30.
 */
public class MailUtils {
    public static void send(String to,String code) throws Exception {
        //发送邮件
        /**
         * 1.*/
        //1.建立一个人会话session
        Properties prop=new Properties();
        //163.邮箱服务器
        prop.setProperty("mail.host","smtp.qq.com");

        //授权
        prop.setProperty("mail.smtp.auth","true");
        prop.setProperty("mail.smtp.port","587");//端口号设置
        Authenticator authenticator=new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //qq 密码是16位口令
                return new PasswordAuthentication("735374446@qq.com","jtmcwanepbvcbdbj");
            }
        };

        Session session=Session.getInstance(prop,authenticator);

        //创建一个-代表邮箱的对象的Message
        Message message=new MimeMessage(session);
        //设置内容
        //从哪来
        message.setFrom(new InternetAddress("735374446@qq.com"));

        //发给谁
       // message.addRecipient(Message.RecipientType.TO,new InternetAddress("wxh199808@163.com"));
        message.addRecipient(Message.RecipientType.CC,new InternetAddress(to));
        //设置标题
        message.setSubject("来自软件1602的邮件");
        //设置内容（格式-html）
        message.setContent("<a href=\"http://localhost:8084/shop/activeCode?code="+code+"\">http://localhost:8084/shop/activeCode?code="+code+"</a>","text/html;charset=utf-8");
        //发送
        Transport.send(message);
    }

}
